package com.ermace.robo.jumper;

import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class LoadingActivity extends Activity {
	
	private ArrayList<Loadable> toLoad;
	private LoadingContent loading;

	private ImageView img;
	
	private LoadingView loadingView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.loadingscreen);
		
        loadingView = (LoadingView) findViewById(R.id.loadingScreenView);
        
        toLoad = getIntent().getExtras().getParcelableArrayList("toLoad");
       
	    loading = new LoadingContent();
		loading.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	return loadingView.handleEvent(event);
    }

	private class LoadingContent extends AsyncTask<URL, Integer, Long> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
	    @Override
		protected Long doInBackground(URL... urls) {
	    	for (Loadable loadable: toLoad)
	    		loadable.load(getResources());
	    	
	    	return null;
	    }

	    @Override
		protected void onProgressUpdate(Integer... progress) {
	    	
	    }

	    @Override
		protected void onPostExecute(Long result) {
	    	Intent returnIntent = new Intent();
	    	returnIntent.putParcelableArrayListExtra("loaded", toLoad);
	    	setResult(1, returnIntent);
	    	finish();
	    }
	}
}
